//
//  PhotoDataSource.m
//  ReallyBigPhotoLibrary
//
//  Created by Kirby Turner on 9/14/10.
//  Copyright 2010 White Peak Software Inc. All rights reserved.
//

#import "PhotoDataSource.h"


@implementation PhotoDataSource

- (void)dealloc
{
    [data_ release], data_ = nil;
    [super dealloc];
}

- (id)init:(NSString *) filePath
{
    self = [super init];
    if (self) {
        data_ = [[NSMutableArray alloc] init]; 
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //在这里获取应用程序Documents文件夹里的文件及文件夹列表
        NSString *documentDir = filePath;
        NSError *error = nil;
        NSArray *fileList = [[NSArray alloc] init];
        //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
        fileList = [fileManager contentsOfDirectoryAtPath:filePath error:&error];
        for(NSString *imageName in fileList){
            if([imageName hasSuffix:@"png"]||[imageName hasSuffix:@"jpg"]){
                NSString *tempStr=[documentDir stringByAppendingPathComponent:imageName];
                NSDictionary *attributes = [[NSFileManager defaultManager] fileAttributesAtPath:tempStr traverseLink:NO];
                BOOL isDirectory=[[attributes objectForKey:NSFileType] isEqualToString:NSFileTypeDirectory];
                if (!isDirectory) {
                    NSString *filePath=[documentDir stringByAppendingPathComponent:imageName];
                    NSData *myData=[fileManager contentsAtPath:filePath];
                    UIImage *myImage=[UIImage imageWithData:myData];
                    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:myImage, @"fullsize", nil];
                    [data_ addObject:dict];
                }
            }
        }
    }
    return self;
}

- (NSInteger)numberOfPhotos
{
    NSInteger count = [data_ count];
    
    return count;
}

// Implement either these, for synchronous images…
- (UIImage *)imageAtIndex:(NSInteger)index
{
    NSDictionary *dict = [data_ objectAtIndex:index];
    UIImage *image = [dict objectForKey:@"fullsize"];
    return image;
}

- (UIImage *)thumbImageAtIndex:(NSInteger)index
{
    NSDictionary *dict = [data_ objectAtIndex:0];
    UIImage *image = [dict objectForKey:@"thumbnail"];
    return image;
}


@end
